#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if defined(TEST_TARGET_vlog)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/vlog.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "vlog.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

#define DEFAULT_OFFLINEFILE "./test/file/log.txt"

static void vlog_callback(char *buf, int len)
{
    printf("vlog_callback[%d]: %s", len, buf);
}

static void test_base(void)
{
    vlog(VLOG_CHANNEL_0,   "[VLOG_CHANNEL_0] vlog!\r\n");
    vlog(VLOG_CHANNEL_1,   "[VLOG_CHANNEL_1] vlog!\r\n");
    vlog(VLOG_CHANNEL_2,   "[VLOG_CHANNEL_2] vlog!\r\n");
}

static void test_channel(void)
{
    VLOG_DISABALE(VLOG_CHANNEL_0);
    VLOG_ENABALE(VLOG_CHANNEL_1);

    vlog(VLOG_CHANNEL_0,   "[VLOG_CHANNEL_0] vlog!\r\n");
    vlog(VLOG_CHANNEL_1,   "[VLOG_CHANNEL_1] vlog!\r\n");
    vlog(VLOG_CHANNEL_2,   "[VLOG_CHANNEL_2] vlog!\r\n");
}

static void test_offline(void)
{
    vlog_set_offline(VLOG_CHANNEL_0, DEFAULT_OFFLINEFILE);

    for (int i = 0; i < 10; i++)
    {
        vlog(VLOG_CHANNEL_0, "vlog %d!\r\n", i);
    }

    vlog_set_offline(VLOG_CHANNEL_0, NULL);
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: vlog [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        ...\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"

"    -c [<channel>]      Select the output channel and select by bit, channel 0 by default\n"
"                        1: channel 0\n"
"                        2: channel 1\n"
"                        4: channel 2\n"
"                        8: channel 3\n"
"                        ...\n"
"    -f [<offline file>] Select the offline out file name, \"./test/file/log.txt\" by default\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"\n"
"argument:\n"
"    <base>              Test base function\n"
"    <channel>           Test channel function\n"
"    <offline>           Test offline function\n"
    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;
    char *logfile = DEFAULT_OFFLINEFILE;
    vlogChnType chn = VLOG_CHANNEL_0;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::c:f::");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'c' :
            chn = atoi(command_optarg);
            break;
        case 'f' :
            if (command_optarg)
            {
                logfile = command_optarg;
            }
            else  
            {
                logfile = DEFAULT_OFFLINEFILE;
            }
            break;
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("vlog version %d.%d.%d\r\n", VLOG_V_MAJOR, VLOG_V_MINOR, VLOG_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            #if defined(TEST_TARGET_vlog)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
        else if (!strcmp(execute, "channel"))
        {
            test_channel();
        }
        else if (!strcmp(execute, "offline"))
        {
            test_offline();
        }
    }
    else  
    {
        if (logfile) vlog_set_offline(chn, logfile);
        vlog(chn, "Hello vlog!\r\n");
        if (logfile) vlog_set_offline(chn, NULL);
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_vlog)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_vlog(void)
{
    command_export("vlog", test);
}
init_export_app(test_vlog);
#endif 